package egoc

import (
	"net/http"
)

type Context struct {
	Service string
	// Argument interface{}
	Request *http.Request
	Writer  http.ResponseWriter

	data  map[string]interface{}
	extra map[string]interface{}
}

func (c *Context) Contains(key string) bool {
	if c.data == nil {
		return false
	}
	_, ok := c.data[key]
	return ok
}

// 添加额外的反馈数据
func (c *Context) PutExtra(key string, extra interface{}) {
	if c.extra == nil {
		c.extra = map[string]interface{}{}
	}
	c.extra[key] = extra
}

// GetExtra 获取额外反馈数据
func (c *Context) GetExtra() map[string]interface{} {
	return c.extra
}

// Get 获取 service 间传递的数据
func (c *Context) Get(key string) interface{} {
	if c.data == nil {
		return key
	}
	return c.data[key]
}

// Put 添加 service 间传递的数据
func (c *Context) Put(key string, data interface{}) {
	if c.data == nil {
		c.data = map[string]interface{}{}
	}
	c.data[key] = data
}

// GetUpload 获取上传文件的信息
func (c *Context) GetUpload() *Upload {
	return &Upload{c.Request}
}
